//
//  PointClassTableViewCell4.m
//  fitnessmanage
//
//  Created by wujian on 2018/1/22.
//  Copyright © 2018年 zongquankeji. All rights reserved.
//

#import "PointClassTableViewCell4.h"

@implementation PointClassTableViewCell4

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        /**
         头像
         */
        UIImageView *headImageView = [[UIImageView alloc] init];
        headImageView.image = PlaceHold;
        headImageView.layer.cornerRadius = 25;
        headImageView.layer.masksToBounds = YES;
        [self.contentView addSubview:headImageView];
        self.headImageView = headImageView;
        
        /**
         昵称
         */
        UILabel *nickNameLabel = [[UILabel alloc] init];
        nickNameLabel.font = MidBigFont;
        nickNameLabel.textColor = CSColorFromRGB(0x000000);
        [self.contentView addSubview:nickNameLabel];
        self.nickNameLabel = nickNameLabel;
        
        
        /**
         性别
         
         */
        UIButton *sexBtn = [[UIButton alloc] init];
        [sexBtn setImage:@"icon_female"];
        [sexBtn setSelectedImage:@"icon_male"];
        [self.contentView addSubview:sexBtn];
        self.sexBtn = sexBtn;
        
        
        /**
         电话
         */
        UILabel *detailLabel = [[UILabel alloc] init];
        detailLabel.font = [UIFont systemFontOfSize:13.0];;
        detailLabel.textColor = CSColorFromRGB(0x9e9e9e);
        [self.contentView addSubview:detailLabel];
        self.detailLabel = detailLabel;
        
        
        UIButton *stateBtn = [[UIButton alloc] init];
        [stateBtn setTitle:@"已完成"];
        [stateBtn setSelectedTitle:@"取消预约"];
        [stateBtn setTitleColor:CSColorFromRGB(0x9e9e9e)];
        [stateBtn setSelectedTitleColor:CSColorFromRGB(0x0a0a0a)];
        stateBtn.titleLabel.font = SmallFont;
        stateBtn.contentEdgeInsets = UIEdgeInsetsMake(5, 8, 5, 8);
        [self.contentView addSubview:stateBtn];
        self.stateBtn = stateBtn;
        
        
        [self setConstraints];
    }
    
    return self;
}


-(void)setConstraints{
    
    /**
     头像
     */
    [self.headImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.contentView).offset(10);
        
        make.top.mas_equalTo(self.contentView).offset(15);
        
        make.width.height.mas_equalTo(50);
        
        make.bottom.mas_equalTo(self.contentView).offset(-15);
    }];
    
    /**
     昵称
     */
    [self.nickNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.headImageView);
        
        make.left.mas_equalTo(self.headImageView.mas_right).offset(15);
    }];
    
    /**
     性别
     
     */
    [self.sexBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.nickNameLabel);
        
        make.left.mas_equalTo(self.nickNameLabel.mas_right).offset(5);
    }];
    
    
    /**
     电话
     */
    [self.detailLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.nickNameLabel.mas_bottom).offset(10);
        
        make.left.mas_equalTo(self.nickNameLabel);
    }];
    
    
    [self.stateBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.contentView);
        
        make.right.mas_equalTo(self.contentView).offset(-10);
    }];
    
}



-(void)setBegin:(NSString *)beginTime{
    
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    
    if ([self compareOneDay:beginTime withAnotherDay:[dateFormatter stringFromDate:[NSDate date]]] > 0) {
        
        //可以取消
        self.stateBtn.selected = YES;
        self.stateBtn.layer.borderWidth = 1.0;
        self.stateBtn.layer.borderColor = CSColorFromRGB(0x0a0a0a).CGColor;
        self.stateBtn.userInteractionEnabled = YES;
        
    }
    else{
        
        //可以取消
        self.stateBtn.selected = NO;
        self.stateBtn.layer.borderWidth = 1.0;
        self.stateBtn.layer.borderColor = BackGroundColor.CGColor;
        self.stateBtn.userInteractionEnabled = NO;
        
    }
    
}


-(int)compareOneDay:(NSString *)oneDay withAnotherDay:(NSString *)anotherDay
{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *dateA = [dateFormatter dateFromString:oneDay];
    
    NSDate *dateB = [dateFormatter dateFromString:anotherDay];
    
    NSComparisonResult result = [dateA compare:dateB];
    
    //NSLog(@"date1 : %@, date2 : %@", oneDay, anotherDay);
    
    if (result == NSOrderedDescending) {
        
        //NSLog(@"Date1  is in the future");
        
        return 1;
        
    }
    
    else if (result ==NSOrderedAscending){
        
        //NSLog(@"Date1 is in the past");
        
        return -1;
        
    }
    
    //NSLog(@"Both dates are the same");
    
    return 0;
    
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextBeginPath(context);
    
    CGContextSetLineWidth(context,1.0);//线宽度
    
    CGContextSetStrokeColorWithColor(context,LineBackGroundColor.CGColor);
    
    CGContextMoveToPoint(context,self.headImageView.left,rect.size.height);
    
    CGContextAddLineToPoint(context,SCREEN_WIDTH,rect.size.height);
    
    CGContextStrokePath(context);
}

@end
